package com.letv.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoadDict {
	
	protected final static Logger logger = LoggerFactory.getLogger(LoadDict.class);
	
	public static Map<String, String> chinesePyMap = new HashMap<String, String>();
	public static Map<String, String> starPyMap = new HashMap<String, String>();
	
	/***
	 * @author GaoJiawei
	 * @param path
	 */
	public static void initPinyinMap(String path) {
		String fullpath = path + File.separatorChar + "album.dict";
		logger.info("专辑拼音字典开始生成……" + fullpath);
		try {
			File f = new File(fullpath);
			FileInputStream fs = new FileInputStream(f);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fs, "UTF-8"));
			String line = null;
			// refreshChinesePyMap();
			while (null != (line = bufferedReader.readLine())) {
				// System.out.println(line);
				String[] words = line.split("@@");
				if (words.length >= 4) {
					chinesePyMap.put(words[0], words[2] + "@@" + words[3]);
				}
			}
		} catch (FileNotFoundException e) {
			logger.error("找不到文件", e);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	/***
	 * @author GaoJiawei
	 * @param path
	 */
	public static void initStarPinyinMap(String path) {
		String fullpath = path + File.separatorChar + "star.dict";
		logger.info("明星拼音字典开始生成……" + fullpath);
		try {
			File f = new File(fullpath);
			FileInputStream fs = new FileInputStream(f);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fs, "UTF-8"));
			String line = null;
			
			while (null != (line = bufferedReader.readLine())) {
				// System.out.println(line);
				String[] words = line.split("@@");
				if (words.length >= 4) {
					starPyMap.put(words[0], words[2] + "@@" + words[3]);
				}
			}
		} catch (FileNotFoundException e) {
			logger.error("找不到文件", e);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
	
	public static void main(String[] args) {
		LoadDict.initPinyinMap("E:\\wanglai");
		LoadDict.initStarPinyinMap("E:\\wanglai");
		System.out.println(LoadDict.chinesePyMap.size());
		System.out.println(LoadDict.starPyMap.size());
		String pinyin = LoadDict.chinesePyMap.get("73868");
		if (null != pinyin && !"".equals(pinyin)) {
			String[] words = pinyin.split("@@");
			//album.setNameJianpin(words[0]); album.setNameQuanpin(words[1]);
			System.out.println(words[0] + " " + words[1]);
		}
		String str = "81196@@笑傲江湖@@xajh@@xiaoaojianghu@@1";
		System.out.println(str.split("@@").length);
	}
}